package com.zw.mes.module.system.service;

import com.zw.mes.constant.Constant;
import com.zw.mes.entity.sys.Dict;
import com.zw.mes.module.system.mapper.DictMapper;
import com.zw.mes.module.system.repository.DictRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.zw.mes.constant.Constant.STATUS_NORMAL;
import static com.zw.mes.util.BeanUtil.copy;


/**
 * @Description:
 *
 * @Author: China.sgl
 * @Date: 2019/12/6 08:42
 */
@Service
@SuppressWarnings("all")
public class DictService {

    @Autowired
    private DictMapper dictMapper;
    @Autowired
    private DictRepository dictRepository;

    /** 按条件查询字典数据 */
    public List<Dict> findDictByCondition(Dict dict) {
        return dictMapper.findByCondition(dict);
    }

    /** 保存字典数据信息 */
    public Dict saveDict(Dict dict) {
        dict.setStatus(STATUS_NORMAL);
        dict.setCreateTime(new Date());
        return dictRepository.save(dict);
    }

    /** 修改字典数据 */
    public Dict updateDict(Dict dict) {
        Dict dictCommon = dictRepository.getOne(dict.getId());
        copy(dict, dictCommon);
        return dictRepository.saveAndFlush(dictCommon);
    }

    /** 删除字典数据 */
    public int delDict(Integer id) {
        return dictMapper.updateStatusById(id, Constant.STATUS_DELETE);
    }

    /** 按分类查询字典数据 */
    public List<Dict> findDictByGroupName(String groupName) {
        return dictMapper.findByGroupName(groupName);
    }

    /**按提报计划部门分类查询字典表类型*/
    public List<Dict> findByGroupNameDept(String groupName){
        return dictMapper.findByGroupNameDept(groupName);
    }
}
